package com.example.myys.data.repository

import com.alibaba.fastjson2.JSON
import com.alibaba.fastjson2.JSONObject
import com.example.myys.Constants
import com.example.myys.MyApplication
import com.example.myys.data.InventoryDatabase
import com.example.myys.data.entity.DataBean
import com.example.myys.data.entity.ReadRecordBean
import com.example.myys.network.PluginInterface
import com.example.myys.util.DateUtil
import com.example.myys.util.FileUtil
import java.util.Date

object MovieRepository {
    //初始化插件接口信息
    var instance: PluginInterface
    var dataJson: JSONObject
    var baseUrl: String

    init {
        val isChecked = SettingLinkRepository.getSettingByTypeIsChecked(Constants.YS)
        dataJson =
            if (isChecked != null) JSON.parseObject(isChecked.value) else JSON.parseArray(
                FileUtil.fileToJsonStr("ysjk.json")
            ).getJSONObject(0)

        val className = "com.example.myys.network.ysPlugins." + dataJson.getString("spiderclass")
        val clazz = Class.forName(className)
        instance = (clazz.newInstance() as? PluginInterface)!!
        baseUrl = dataJson.getString("url")
    }

    suspend fun initObject() {
        val isChecked = SettingLinkRepository.getSettingByTypeIsChecked(Constants.YS)
        dataJson =
            if (isChecked != null) JSON.parseObject(isChecked.value) else JSON.parseArray(
                FileUtil.fileToJsonStr("ysjk.json")
            ).getJSONObject(0)

        val className = "com.example.myys.network.ysPlugins." + dataJson.getString("spiderclass")
        val clazz = Class.forName(className)
        instance = (clazz.newInstance() as? PluginInterface)!!
        baseUrl = dataJson.getString("url")
    }

    val movieDao= InventoryDatabase.getInstance(MyApplication.instance).dataBeanDao()
    val chapterDao= InventoryDatabase.getInstance(MyApplication.instance).chapterDao()
    val recordDao = InventoryDatabase.getInstance(MyApplication.instance).readRecordDao()
    val TYPE= Constants.YS


    //获取影视推荐
    suspend fun getMovieRecommend():List<DataBean>?{
        val recommendJson = dataJson.getJSONObject("recommend")
        val movieList = instance.getRecommend(baseUrl, TYPE, recommendJson)
        return movieList
    }

    //分类获取信息
    suspend fun getMovieByType(type: String, page: Int): List<DataBean>? {
        val categorizeJson = dataJson.getJSONObject("categorize")
        return instance.getCategorize(baseUrl, type, TYPE, page, categorizeJson)
    }

    //获取电影详情
    suspend fun getDetails(dataBean: DataBean): DataBean {
        val detailsJson = dataJson.getJSONObject("details")
        return instance.getDetails(baseUrl, dataBean, detailsJson)
    }
    //搜索电影
    suspend fun getMovieByKeyword(keyword:String):List<DataBean>?{
        val searchkeywordJson = dataJson.getJSONObject("searchkeyword")
        return instance.getSearchKeyWord(baseUrl, TYPE, keyword, searchkeywordJson)
    }

    //获取播放
    suspend fun getContent(url: String): String {
        val contentJson = dataJson.getJSONObject("content")
        return instance.getContent(baseUrl, url, contentJson)
    }
    //加入收藏
    suspend fun addMovieShelf(dataBean:DataBean){
        dataBean.type=TYPE
        dataBean.browseTime = DateUtil.getDateStr(Date())
        dataBean.dataSourceName = dataJson.getString("name")
        val moives = movieDao.getByNameType(dataBean.name, dataBean.type)
        if (moives.isEmpty()) {
            val id = movieDao.insert(dataBean)
            val record = ReadRecordBean(
                dataBeanId = id.toInt(),
                chapterPos = 0,
                pagePos = 0,
                lastRead = Date().toString(),
                type = TYPE
            )
            recordDao.insert(record)
        } else {
            dataBean.id = moives[0].id
            movieDao.update(dataBean)
        }
    }
    //获取收藏
    suspend fun getMovieShelf():List<DataBean>{
        return movieDao.getListByType(TYPE)
    }
    //删除收藏
    suspend fun deleteShelf(dataBean: DataBean) {
        val recordList = recordDao.getListDataBeanId(dataBean.id, TYPE)
        recordDao.deleteList(recordList)
        movieDao.delete(dataBean)
    }
    //保存观看记录
    suspend fun saveRecord(record: ReadRecordBean, isUpdate: Boolean) {
        if (isUpdate) {
            recordDao.update(record)
        } else {
            record.type = TYPE
            recordDao.insert(record)
        }
    }
    //获取观看记录
    suspend fun getRecord(dataId: Int): ReadRecordBean {
        val listDataBeanId = recordDao.getListDataBeanId(dataId, TYPE)
        if (listDataBeanId.isEmpty()) {
            return ReadRecordBean()
        }
        return recordDao.getListDataBeanId(dataId, TYPE)[0]
    }
}